package com.azoft.carousellayoutmanager.sample.slice;

import com.azoft.carousellayoutmanager.*;
import com.azoft.carousellayoutmanager.sample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.service.WindowManager;

import static com.azoft.carousellayoutmanager.Constant.*;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private CarouselLayoutManager carouselLayoutManagerVertical;
    private CarouselLayoutManager carouselLayoutManagerHorizontal;

    /**
     * 加载布局
     *
     * @param intent 启动的intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(TITLE_COLOR);
        super.setUIContent(ResourceTable.Layout_manager_layout);
        initView();
        initOtherView();
    }

    /**
     * 初始化按钮控件
     */
    private void initOtherView() {
        DependentLayout dirLayoutClickForward = (DependentLayout) findComponentById(
                ResourceTable.Id_dir_layout_click_forward);
        DependentLayout dirLayoutClickBackward = (DependentLayout) findComponentById(
                ResourceTable.Id_dir_layout_click_backward);
        dirLayoutClickForward.setClickedListener(this);
        dirLayoutClickBackward.setClickedListener(this);
    }

    /**
     * 初始化布局管理器控件
     */
    private void initView() {
        CustomAdapterImp customAdapterImp = new CustomAdapterImp(this);
        carouselLayoutManagerVertical = (CarouselLayoutManager) findComponentById(
                ResourceTable.Id_custom_manager_view_vertical);
        carouselLayoutManagerVertical.setOrientation(Component.VERTICAL);
        carouselLayoutManagerVertical.setAdapter(customAdapterImp);
        DefaultChildSelectionListener.initCenterItemListener(this::showClickPosition, carouselLayoutManagerVertical);
        carouselLayoutManagerHorizontal = (CarouselLayoutManager) findComponentById(
                ResourceTable.Id_custom_manager_view_horizontal);
        carouselLayoutManagerHorizontal.setOrientation(Component.HORIZONTAL);
        carouselLayoutManagerHorizontal.setAdapter(customAdapterImp);
        DefaultChildSelectionListener.initCenterItemListener(this::showClickPosition, carouselLayoutManagerHorizontal);
    }

    /**
     * toast展示选中item的下标
     *
     * @param position 下标
     */
    private void showClickPosition(int position) {
        ShowItemClickPositionDialog showItemClickPositionDialog = new ShowItemClickPositionDialog(this);
        showItemClickPositionDialog.setText(position).show();
    }

    /**
     * 点击事件
     *
     * @param component 控件
     */
    @Override
    public void onClick(Component component) {
        int id = component.getId();
        switch (id) {
            case ResourceTable.Id_dir_layout_click_backward:
                clickBackward();
                break;
            case ResourceTable.Id_dir_layout_click_forward:
                clickForward();
                break;
            default:
                break;
        }
    }

    /**
     * 点击向后的按钮
     */
    private void clickBackward() {
        carouselLayoutManagerVertical.setBack(true);
        carouselLayoutManagerHorizontal.setHorizontalLeft(true);
        carouselLayoutManagerHorizontal.setHorizontalRight(false);
        carouselLayoutManagerHorizontal.setBack(true);
        int end = carouselLayoutManagerVertical.getCustomAdapterImp().getCount() - INDEX;
        if (end > carouselLayoutManagerHorizontal.getIndex()) {
            carouselLayoutManagerHorizontal.setHorizontalBoundary(true);
            carouselLayoutManagerHorizontal.sendRunnable(end, true);
        } else if (end < carouselLayoutManagerHorizontal.getIndex()) {
            carouselLayoutManagerHorizontal.setHorizontalBoundary(false);
            carouselLayoutManagerHorizontal.sendRunnable(end, true);
        }
        int center = carouselLayoutManagerVertical.getIndex();
        if (center == end) {
            return;
        }
        if (center <= ALL_DATA_CENTER || center == carouselLayoutManagerVertical.getCustomAdapterImp().getCount() - 1) {
            carouselLayoutManagerVertical.setVerticalBottom(true);
            carouselLayoutManagerVertical.setVerticalTop(false);
        } else {
            carouselLayoutManagerVertical.setVerticalBottom(false);
            carouselLayoutManagerVertical.setVerticalTop(true);
        }
        carouselLayoutManagerVertical.sendRunnable(end, true);
    }

    /**
     * 点击向前的按钮
     */
    private void clickForward() {
        carouselLayoutManagerVertical.setVerticalBottom(false);
        carouselLayoutManagerVertical.setVerticalTop(true);
        carouselLayoutManagerVertical.setBack(false);
        carouselLayoutManagerHorizontal.setHorizontalLeft(false);
        carouselLayoutManagerHorizontal.setHorizontalRight(true);
        carouselLayoutManagerHorizontal.setBack(false);
        int start = 1;
        if (start > carouselLayoutManagerHorizontal.getIndex()) {
            carouselLayoutManagerHorizontal.setHorizontalBoundary(true);
            carouselLayoutManagerHorizontal.sendRunnable(start, true);
        } else if (start < carouselLayoutManagerHorizontal.getIndex()) {
            carouselLayoutManagerHorizontal.setHorizontalBoundary(false);
            carouselLayoutManagerHorizontal.sendRunnable(start, true);
        }
        int startCenter = carouselLayoutManagerVertical.getIndex();
        if (startCenter == start) {
            return;
        }
        if (startCenter <= ALL_DATA_CENTER && startCenter > 0) {
            carouselLayoutManagerVertical.setVerticalBottom(true);
            carouselLayoutManagerVertical.setVerticalTop(false);
        } else {
            carouselLayoutManagerVertical.setVerticalBottom(false);
            carouselLayoutManagerVertical.setVerticalTop(true);
        }
        carouselLayoutManagerVertical.sendRunnable(start, true);
    }
}
